/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package DataAccess;

import Entity.Account;
import Servlet.PMF;
import com.google.appengine.api.datastore.Key;
import java.util.List;
import javax.jdo.PersistenceManager;
import javax.jdo.Query;

/**
 *
 * @author phuc
 */
public class UserAccess {
    PersistenceManager pm;
    public UserAccess(){
        pm = PMF.get().getPersistenceManager();
    }
    public Boolean addUser(String username, String password, String name, String role)
    {
        try{
                    if (username.trim() != "")
                    {
                        Account acc = new Account(username, password, name, role);
                        pm.makePersistent(acc);
                        //response.sendRedirect("/Account/DisplayAccount.jsp");
                        return true;
                    }
                    else
                        return false;

                }
                catch(Exception e){
                     return false;
                }
                finally
                {
                    pm.close();
                }

    }

    public Boolean checkExistedUser()
    {
        try{
            Query q = pm.newQuery(Account.class);
            List<Account> results = (List<Account>)q.execute();
            if (results.size() > 0)
            {
               return true;
            }
            else
               return false;
        }catch(Exception e)
        {
           return false;
        }
        finally
        {
           pm.close();
        }
    }
    public Account getUserByID(Long ID){
        Account acc = new Account();
       
        Query q = pm.newQuery(Account.class,"id == uid");
        q.declareParameters("Long uid");
        List<Account> results = (List<Account>)q.execute(ID);
        acc = results.get(0);
        if (results.size() > 0)
        {
            return acc;
        }
        return null;
    }
    public Boolean changePassword(Long ID,String oldpass, String newpass)
    {
         pm = PMF.get().getPersistenceManager();
         Account acc = pm.getObjectById(Account.class, ID);
         if (newpass.equals(""))
         {           
             return false;
         }
         else if (!oldpass.equals(acc.getPassword()))
             return false;         
         else
         {
             acc.setPassword(newpass);
             pm.close();
             return true;
         }
    }
    public Boolean editUser(Long ID, String password, String role, String fullname)
    {
        pm = PMF.get().getPersistenceManager();
        Account acc = pm.getObjectById(Account.class, ID);

        // if password = "" -> don't change password
        if (!password.equals(""))
            acc.setPassword(password);
        acc.setRole(role);
        acc.setFullname(fullname);

        pm.close();
        return true;
    }
    public List<Account> getListUser()
    {

        pm = PMF.get().getPersistenceManager();
        String query = "select from " + Account.class.getName();
        List<Account> list = (List<Account>)pm.newQuery(query).execute();
        //pm.close();
        return list;
    }
    public List<Account> getListUser(String role)
    {
        Account acc = new Account();

        Query q = pm.newQuery(Account.class,"role == r");
        q.declareParameters("String r");
        List<Account> results = (List<Account>)q.execute(role);

        return results;
    }
    public void deleteUserByID(Long ID){
        Account acc = getUserByID(ID);
        pm.deletePersistent(acc);        
    }
    public String checkPermision(Long ID)
    {
        String rule = "";
        Account acc = new Account();
        acc = getUserByID(ID);
        rule = acc.getRole().trim();
        return rule;
    }
}
